package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.kaishengit.entity.Document;
import com.kaishengit.entity.User;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.Page;
import com.kaishengit.util.db.RowMapper;


public class DocumentDao {

	public DBHelp<Document> db = new DBHelp<Document>();
	public void save(Document document){
		String sql = "insert into t_document(id,title,content,userId,projectId,createtime) values (?,?,?,?,?,?)";
		db.executeSQL(sql, document.getId(),document.getTitle(),document.getContent(),document.getUserId(),document.getProjectId(),document.getCreatetime());
	}
	
	public void update(Document document){
		String sql = "update t_document set title = ?, content = ?,userId = ?, projectId = ?,createtime = ? where id = ?";
		db.executeSQL(sql, document.getTitle(),document.getContent(),document.getUserId(),document.getProjectId(),document.getCreatetime(),document.getId());
	}
	
	public void del(int id){
		String sql = "delete from t_document where id = ?";
		db.executeSQL(sql, id);
	}
	
	public Document  findById(int id){
		String sql = "select * from t_document where id = ?";
		return db.executeQueryToObject(sql, new RowMapper<Document>() {

			public Document mapRow(ResultSet resultSet) throws SQLException {
				Document document = new Document();
				document.setId(resultSet.getInt("id"));
				document.setTitle(resultSet.getString("title"));
				document.setContent(resultSet.getString("content"));
				document.setCreatetime(resultSet.getString("createtime"));
				document.setUserId(resultSet.getInt("userId"));
				document.setProjectId(resultSet.getInt("projectId"));
				User user = new UserDao().findById(resultSet.getInt("userId"));
				document.setUser(user);
				return document;
			}
		}, id);
	}
	
	private class DocumentRowMapper implements RowMapper<Document>{

		public Document mapRow(ResultSet rs) throws SQLException {
			Document document = new Document();
			document.setId(rs.getInt("id"));
			document.setTitle(rs.getString("title"));
			document.setContent(rs.getString("content"));
			document.setUserId(rs.getInt("userId"));
			document.setProjectId(rs.getInt("projectId"));
			document.setCreatetime(rs.getString("createtime"));
			return document;
		}
	}
		
		
	public Page<Document> findCommentByProjectId(int nowPageNum, int id) {
		
		Page<Document> page = new Page<Document>(nowPageNum,countByProjectId(id));
		String sql = "select * from t_document where projectId=? order by createtime desc limit ?,?";
		List<Document> list = db.executeQueryToList(sql, new RowMapper<Document>() {

			public Document mapRow(ResultSet resultSet) throws SQLException {
				Document document = new Document();
				document.setId(resultSet.getInt("id"));
				document.setTitle(resultSet.getString("title"));
				document.setContent(resultSet.getString("content"));
				document.setCreatetime(resultSet.getString("createtime"));
				document.setUserId(resultSet.getInt("userId"));
				document.setProjectId(resultSet.getInt("projectId"));
				System.out.println("bbbbbb"+resultSet.getInt("userId"));
				User user = new UserDao().findById(resultSet.getInt("userId"));
				System.out.println(user);
				document.setUser(user);
				return document;
			}
		}, id,page.getStartPage(),page.getPagesize());
		page.setList(list);
		
		return page;
	}

	private int countByProjectId(int pid) {
		String sql="select count(*) from t_document where projectId=?";
		return db.executeQuery(sql, pid).intValue();
	}
	
	public Document findByIdonly(int id) {
		String sql = "select * from t_document where id = ?";
		return db.executeQueryToObject(sql, new DocumentRowMapper(),id);
	}

	public void removeById(int id) {
		String sql="delete from t_document where id=?";
		db.executeSQL(sql, id);
	}

	
}
